package com.FH.organization.diarytask_manager;


import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by wlb on 2016/7/15.
 * <p>
 * 日程管理
 */
@Stateless
public class DiaryTaskDAOImpl extends AccessDB implements DiaryTaskDAO {

    @Override
    public ResultDTO getDiaryTaskInfo(String serach, String page, String size) {

/**
 * 日程任务管理
 */
        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(serach);
            String existName = "0";

            String sqlexit = "select count(1) from schedule  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("member_id")) {
                sql_w += " AND member_id = '" + json.getString("member_id") + "'";
            }
            if (json.has("schedule_title")) {
                sql_w += " AND schedule_title like '%" + json.getString("schedule_title") + "%'";
            }

            if (json.has("schedule_id")) {
                sql_w += " AND schedule_id ='" + json.getString("schedule_id") + "'";
            }

            if (json.has("type")) {
                sql_w += " AND type ='" + json.getString("type") + "'";
            }

            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<DiaryTaskShowDTO> result = new ArrayList<DiaryTaskShowDTO>();

            if (!existName.equals("0")) {
                String sqlq = "select schedule_id,schedule_title,schedule_addr,schedule_desc, schedule_starttime,schedule_endtime,schedule_status,isverify,member_id,type,meeting_room_id FROM schedule where 1=1  " + sql_w + " ORDER BY time DESC" + sql_page;
                // String sql ="select * from finance_budget" ;
                seresult = this.getReportingTo(sqlq);
                //  System.out.println("tttttttt");
                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    DiaryTaskShowDTO diaryTaskShowDTO = new DiaryTaskShowDTO();

                    diaryTaskShowDTO.setSchedule_id(seresult.get(i)[1]);
                    diaryTaskShowDTO.setSchedule_title(seresult.get(i)[2]);
                    diaryTaskShowDTO.setSchedule_addr(seresult.get(i)[3]);
                    diaryTaskShowDTO.setSchedule_desc(seresult.get(i)[4]);
                    String starttime = seresult.get(i)[5];
                    String endtime = seresult.get(i)[6];
//                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s.S");
//                    SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd H:m:s");
//                    try {
//                        Date startdate = format.parse(starttime);
//                        Date enddate = format.parse(endtime);
//                        starttime = format2.format(startdate);
//                        endtime = format2.format(enddate);
//
//                    } catch (ParseException e) {
//                        e.printStackTrace();
//                    }


                    diaryTaskShowDTO.setSchedule_starttime(starttime);
                    diaryTaskShowDTO.setSchedule_endtime(endtime);
                    diaryTaskShowDTO.setSchedule_status(seresult.get(i)[7]);
                    diaryTaskShowDTO.setIsverify(seresult.get(i)[8]);
                    diaryTaskShowDTO.setMember_id(seresult.get(i)[9]);
                    diaryTaskShowDTO.setType(seresult.get(i)[10]);
                    diaryTaskShowDTO.setMeeting_room_id(seresult.get(i)[11]);
                    result.add(diaryTaskShowDTO);

                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    @Override
    public ResultDTO addDiaryTaskInfo(DiaryTaskPullDTO dataDTO) {

        ResultDTO resultDTO = new ResultDTO();//构建返回对象

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式


        if (dataDTO.getSchedule_id() != null && !dataDTO.getSchedule_id().equals("")) {

            String sql = "select count(1) from schedule where schedule_id='" + dataDTO.getSchedule_id() + "'";

            String exist = getReportingToOne(sql);

            if (!exist.equals("0")) {
                return updateDiaryTaskInfo(dataDTO);
            }
        }

        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        if (null != dataDTO.getUid() && !dataDTO.getUid().equals("")) {
            uuid = dataDTO.getUid();
        }
        String QUERY = "";

        QUERY += "insert into schedule(schedule_id,schedule_title,schedule_addr," +
                "schedule_desc,schedule_starttime," + "schedule_endtime,schedule_status,isverify,member_id,time,type,meeting_room_id) values" +
                " ('" + uuid + "','" + dataDTO.getSchedule_title() + "','" + dataDTO.getSchedule_addr() + "','"
                + dataDTO.getSchedule_desc() + "','" + dataDTO.getSchedule_starttime() + "','"
                + dataDTO.getSchedule_endtime() + "','" + dataDTO.getSchedule_status() + "','0','" + dataDTO.getMember_id() + "','" + simpleDateFormat.format(new Date()) + "','" + dataDTO.getType() + "','" + dataDTO.getMeeting_room_id() + "')";

        int result = this.addReportingTo(QUERY);
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStr(uuid);
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.add_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }


    /**
     * 删除单个列表
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO deleteDiaryTaskInfo(DiaryTaskPullDTO dataDTO) {

        String sqlexit = "select count(1) from schedule where", sql_delete = "delete from schedule where";
        if (null != dataDTO.getSchedule_id() && !"".equals(dataDTO.getSchedule_id())) {
            sqlexit += " schedule_id='" + dataDTO.getSchedule_id() + "'";
            sql_delete += " schedule_id='" + dataDTO.getSchedule_id() + "'";
        }
        if (null != dataDTO.getMeeting_room_id() && !"".equals(dataDTO.getMeeting_room_id())) {
            sqlexit += " meeting_room_id='" + dataDTO.getMeeting_room_id() + "'";
            sql_delete += " meeting_room_id='" + dataDTO.getMeeting_room_id() + "'";
        }

        //删除之前查询是否有记录
        //String sql = "select count(1) from schedule where schedule_id='"+dataDTO.getSchedule_id()+"'";

        String existName = getReportingToOne(sqlexit);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {

            //String QUERY = "delete from schedule where schedule_id='"+dataDTO.getSchedule_id()+"'";

            result = this.addReportingTo(sql_delete);

        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

    /**
     * 修改表数据
     *
     * @param dataDTO
     * @return
     */
    @Override
    public ResultDTO updateDiaryTaskInfo(DiaryTaskPullDTO dataDTO) {
        ResultDTO resultDTO = new ResultDTO();
        int result = 0;

        if (dataDTO.getSchedule_id() != null && !dataDTO.getSchedule_id().equals("")) {

            String sql = "select count(1) from schedule where schedule_id='" + dataDTO.getSchedule_id() + "' ";
            String sum = this.getReportingToOne(sql);
            if (!sum.equals("0")) {
                String update_sql = "update schedule set schedule_status='0'";
                if (null != dataDTO.getSchedule_title() && !"".equals(dataDTO.getSchedule_title())) {
                    update_sql += ",schedule_title='" + dataDTO.getSchedule_title() + "'";
                }
                if (null != dataDTO.getSchedule_addr() && !"".equals(dataDTO.getSchedule_addr())) {
                    update_sql += ",schedule_addr='" + dataDTO.getSchedule_addr() + "'";
                }
                if (null != dataDTO.getSchedule_starttime() && !"".equals(dataDTO.getSchedule_starttime())) {
                    update_sql += ",schedule_starttime='" + dataDTO.getSchedule_starttime() + "'";
                }
                if (null != dataDTO.getSchedule_endtime() && !"".equals(dataDTO.getSchedule_endtime())) {
                    update_sql += ",schedule_endtime='" + dataDTO.getSchedule_endtime() + "'";
                }
                if (null != dataDTO.getSchedule_desc() && !"".equals(dataDTO.getSchedule_desc())) {
                    update_sql += ",schedule_desc='" + dataDTO.getSchedule_desc() + "'";
                }

                update_sql += " where schedule_id='" + dataDTO.getSchedule_id() + "'";

                result = this.addReportingTo(update_sql);

            }
            // 判断SQl是否执行成功
            if (result != 0) {
                resultDTO.setStr(dataDTO.getSchedule_id());
                resultDTO.setStatus("90000");//
                resultDTO.setDescritpion(warnSrc.update_success);
            } else {
                resultDTO.setStatus("90001");//
                resultDTO.setDescritpion(warnSrc.update_fail);
            }
        }
        return resultDTO;
    }
}